<template>
  <div class="visitor-notice">
    <div
      class="page-header"
      :style="bannerImage ? { backgroundImage: `url(${bannerImage})` } : {}"
      :class="{ 'has-bg': !!bannerImage }"
    >
      <div class="container">
        <h1 class="page-title">观众须知</h1>
        <p class="page-subtitle">参观展会的重要信息和注意事项</p>
      </div>
    </div>
    <!-- 导航菜单 -->
    <div class="nav-menu">
      <div class="container">
        <nav class="about-nav">
          <a
            @click="setActiveTab('group-visit')"
            :class="['nav-item', { active: activeTab === 'group-visit' }]"
          >
            组团参观
          </a>
          <a
            @click="setActiveTab('visitor-preregistration')"
            :class="[
              'nav-item',
              { active: activeTab === 'visitor-preregistration' },
            ]"
          >
            观众预登记
          </a>
          <a
            @click="setActiveTab('visitor-notice')"
            :class="['nav-item', { active: activeTab === 'visitor-notice' }]"
          >
            观众须知
          </a>
          <a
            @click="setActiveTab('visa-system')"
            :class="['nav-item', { active: activeTab === 'visa-system' }]"
          >
            签证申请
          </a>
          <a
            @click="setActiveTab('visitor-roster')"
            :class="['nav-item', { active: activeTab === 'visitor-roster' }]"
          >
            展商名录
          </a>
          <a
            @click="setActiveTab('visitor-reviews')"
            :class="['nav-item', { active: activeTab === 'visitor-reviews' }]"
          >
            观众评语
          </a>
        </nav>
      </div>
    </div>

    <div class="container">
      <section class="content-section">
        <!-- <h2 class="section-title">观众须知</h2>
        <p v-if="pageData.description" class="section-desc">
          {{ pageData.description }}
        </p> -->

        <!-- 多条数据展示：图片在上，文本在下 -->
        <div class="notice-grid">
          <div
            class="notice-item"
            v-for="notice in visitorNotices"
            :key="notice.id"
          >
            <!-- 图片在上 -->
            <div class="notice-image-section" v-if="notice.image">
              <img
                :src="notice.image"
                :alt="notice.title"
                class="notice-image"
              />
            </div>
            <!-- 文本在下 -->
            <div class="notice-content">
              <h3 class="notice-title">{{ notice.title }}</h3>
              <div v-html="notice.description" class="notice-description"></div>
            </div>
          </div>
        </div>
      </section>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from "vue";
import { subpageApi } from "@/api/user";

export default {
  name: "VisitorNotice",
  setup() {
    const activeTab = ref("visitor-notice");
    const bannerImage = ref("");
    const loadBanner = async () => {
      try {
        const res = await subpageApi.getContentByType("carousel_visitors");
        if (
          res &&
          (res.code === "200" || res.code === 200) &&
          Array.isArray(res.data) &&
          res.data.length > 0
        ) {
          const firstActive =
            res.data.find((item) => item.status === "active") || res.data[0];
          bannerImage.value = firstActive.imageUrl || firstActive.image || "";
        }
      } catch (e) {
        bannerImage.value = "";
      }
    };
    // 页面基本信息
    const pageData = ref({});

    // 观众须知数据
    const visitorNotices = ref([]);

    // 解析富文本内容
    const parseContent = (content) => {
      if (!content) return { content: "", backgroundImage: "" };

      try {
        if (typeof content === "string" && content.trim().startsWith("{")) {
          return JSON.parse(content);
        }
        return { content: content, backgroundImage: "" };
      } catch (e) {
        return { content: content, backgroundImage: "" };
      }
    };

    // 加载观众须知数据
    const loadVisitorNotice = async () => {
      try {
        const response = await subpageApi.getSubpageList(
          "visitors",
          "visitor-notice"
        );
        if (
          response &&
          response.code === "200" &&
          response.data &&
          response.data.length > 0
        ) {
          // 取第一条作为页面基本信息
          const firstItem = response.data[0];
          pageData.value = {
            title: firstItem.title || "观众须知",
            description:
              firstItem.description ||
              (firstItem.content &&
              !(firstItem.content || "").trim().startsWith("[") &&
              !(firstItem.content || "").trim().startsWith("{")
                ? firstItem.content
                : ""),
          };

          // 处理多条观众须知数据
          visitorNotices.value = response.data.map((item, index) => {
            const parsed = parseContent(item.content);
            return {
              id: item.id || index + 1,
              title: item.title || "",
              description: parsed.content || "",
              image: item.imageUrl || parsed.backgroundImage || "",
            };
          });
        } else {
          // 使用默认数据
          pageData.value = getDefaultPageData();
          visitorNotices.value = getDefaultVisitorNotices();
        }
      } catch (error) {
        console.error("加载观众须知数据失败:", error);
        pageData.value = getDefaultPageData();
        visitorNotices.value = getDefaultVisitorNotices();
      }
    };

    onMounted(() => {
      loadVisitorNotice();
      loadBanner();
    });

    // 默认页面数据
    const getDefaultPageData = () => {
      return {
        title: "观众须知",
        description: "参观展会的重要信息和注意事项，请仔细阅读",
      };
    };

    // 默认观众须知数据
    const getDefaultVisitorNotices = () => {
      return [
        {
          id: 1,
          title: "入场须知",
          description:
            "<p><strong>入场时间：</strong>展会期间每日9:00-17:00开放</p><p><strong>入场方式：</strong>请携带有效身份证件和参观证</p><p><strong>安全检查：</strong>所有观众需通过安全检查后方可入场</p>",
          image:
            "https://images.unsplash.com/photo-1581091226825-a6a2a5aee158?w=400&h=300&fit=crop",
        },
        {
          id: 2,
          title: "参观礼仪",
          description:
            "<p><strong>着装要求：</strong>请着正装或商务休闲装参观</p><p><strong>行为规范：</strong>保持安静，不要大声喧哗</p><p><strong>拍照规定：</strong>部分展区禁止拍照，请遵守现场指示</p>",
          image:
            "https://images.unsplash.com/photo-1551288049-bebda4e38f71?w=400&h=300&fit=crop",
        },
        {
          id: 3,
          title: "安全须知",
          description:
            "<p><strong>紧急情况：</strong>如遇紧急情况，请听从现场工作人员指挥</p><p><strong>个人物品：</strong>请妥善保管个人物品，贵重物品请随身携带</p><p><strong>联系方式：</strong>紧急联系电话：400-123-4567</p>",
          image:
            "https://images.unsplash.com/photo-1565043589221-1a6fd9a6d0b8?w=400&h=300&fit=crop",
        },
        {
          id: 4,
          title: "服务设施",
          description:
            "<p><strong>休息区域：</strong>展馆内设有多个休息区域</p><p><strong>餐饮服务：</strong>提供多种餐饮选择</p><p><strong>咨询服务：</strong>现场设有咨询服务台</p>",
          image:
            "https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=400&h=300&fit=crop",
        },
      ];
    };

    const setActiveTab = (tabKey) => {
      if (!tabKey) return;
      activeTab.value = tabKey;
      if (tabKey !== "visitor-notice") {
        window.location.href = `/${tabKey}`;
      }
    };

    return {
      activeTab,
      bannerImage,
      pageData,
      visitorNotices,
      setActiveTab,
    };
  },
};
</script>

<style scoped>
.visitor-notice {
  min-height: 100vh;
  background: #f8f9fa;
}

:deep(.resizable-image) {
  width: 100% !important;
  max-width: 100% !important;
  height: auto !important;
}
.page-header {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  padding: 120px 0 80px 0;
  margin-top: 0;
  text-align: center;
  color: white;
}

.page-header.has-bg {
  height: 400px;
  position: relative;
  background: none;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.page-header.has-bg::before {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
}

.page-header.has-bg .container {
  position: relative;
  z-index: 1;
}

.page-title {
  font-size: 3rem;
  font-weight: 700;
  margin-bottom: 1rem;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

.page-subtitle {
  font-size: 1.2rem;
  opacity: 0.9;
  margin-bottom: 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.content-section {
  background: white;
  border-radius: 20px;
  padding: 3rem;
  margin: 2rem 0;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.section-title {
  font-size: 2rem;
  color: #333;
  margin-bottom: 2rem;
  text-align: center;
  position: relative;
  padding-left: 15px;
}

.section-title::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 4px;
  height: 24px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 2px;
}

.section-desc {
  margin: -8px 0 2rem;
  color: #666;
  text-align: center;
  font-size: 1.1rem;
  line-height: 1.6;
}

/* 多条数据网格布局 */
.notice-grid {
  display: grid;
  grid-template-columns: repeat(1, 1fr);
  gap: 2rem;
  /* margin-top: 2rem; */
}

.notice-item {
  background: white;
  border-radius: 15px;
  overflow: hidden;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.notice-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.15);
}

/* 图片在上 */
.notice-image-section {
  width: 100%;
  padding-bottom: 60%;
  position: relative;
  overflow: hidden;
}

.notice-image {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* 文本在下 */
.notice-content {
  padding: 1.5rem;
}

.notice-title {
  font-size: 1.3rem;
  font-weight: 600;
  color: #333;
  margin-bottom: 1rem;
  text-align: center;
}

.notice-description {
  color: #666;
  line-height: 1.6;
  text-align: left;
}

/* 富文本内图片限制 */
.notice-description :deep(img) {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 10px 0;
  border-radius: 8px;
}

@media (max-width: 768px) {
  .page-title {
    font-size: 2rem;
  }

  .content-section {
    padding: 2rem;
    margin: 1rem 0;
  }

  .section-title {
    font-size: 1.5rem;
  }

  .notice-grid {
    grid-template-columns: 1fr;
    gap: 1.5rem;
  }
}

/* 导航菜单样式 - 参考关于我们页面 */
.nav-menu {
  background: #f8f9fa;
  padding: 20px 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.about-nav {
  display: flex;
  justify-content: center;
  gap: 30px;
  flex-wrap: wrap;
}

.nav-item {
  text-decoration: none;
  color: #333;
  font-size: 1.1rem;
  font-weight: 500;
  padding: 10px 20px;
  border-radius: 8px;
  transition: background-color 0.3s ease, color 0.3s ease;
  border: 1px solid #eee;
  cursor: pointer;
}

.nav-item:hover {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

.nav-item.active {
  background-color: var(--e-global-color-selected);
  color: white;
  border-color: var(--e-global-color-selected);
}

@media (max-width: 768px) {
  .about-nav {
    gap: 15px;
  }

  .nav-item {
    font-size: 1rem;
    padding: 8px 16px;
  }
}
</style>
